/**************************************************************************
*
* Tint Task Manager
* 
* Copyright (C) 2007 Pål Staurland (staura@gmail.com)
* 
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Soft-
* ware"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following condi-
* tions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* 
**************************************************************************/

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include "event.h"
#include "server.h"
#include "visual.h"
#include "config.h"
#include "task.h"
#include "window.h"

void refresh_visual ()
{
	server_refresh_root_pixmap ();
	visual_draw_tasks ();
	server_refresh_main_pixmap ();
}

void set_refresh_all ()
{
	Task *tsk;

	tsk = task.tasklist;
	while (tsk)
	{
		tsk->need_refresh = 1;
		tsk = tsk->next;
	}
}

void event_destroy_notify (Window win)
{
	task_remove_task (win);
	if (config.panel_tasks_centered)
		set_refresh_all ();
	refresh_visual ();
}

void event_button_press (int button, int x, int y)
{
	Task *tsk;
	tsk = task.tasklist;
	while (tsk)
	{
		if (x >= tsk->pos_x && x <= tsk->pos_x + config.task_width)
		{
			switch (button)
			{
				case 1: 
				case 4:
				case 5:
					window_toggle_iconify (tsk->win);
					break;
					
				case 2:
					XDestroyWindow (server.dsp, tsk->win);
					break;
				
				case 3:
					window_toggle_shade (tsk->win);
					break;
			}
		}
		tsk = tsk->next;
	}

	/* Fix to keep window below */
	XLowerWindow (server.dsp, window.main_win);
}

void event_expose ()
{
	server_refresh_main_pixmap ();
}

void event_property_notify (Window win, Atom at)
{
	//printf("atom= %s\n", XGetAtomName(server.dsp, at));

	if (win == server.root_win)
	{
		if (!server.got_root_win)
		{
			XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
			server.got_root_win = 1;
		}
		
		/* New desktop */
		if (at == server.atom._NET_CURRENT_DESKTOP)
		{
			task_new_desktop ();
			task_refresh_tasklist ();
			refresh_visual ();
		}
			
		/* Window list */
		else if (at == server.atom._NET_CLIENT_LIST)
		{
			task_refresh_tasklist ();
			if (config.panel_tasks_centered)
				set_refresh_all ();
			refresh_visual ();
		}
		
		/* Active */
		else if (at == server.atom._NET_ACTIVE_WINDOW)
		{
			Window win;
			Task *tsk;
			
			win = window_get_active ();
			tsk = task.tasklist;
			while (tsk)
			{
				tsk->active = 0;
				if (tsk->win == win)
					tsk->active = 1;	
				tsk = tsk->next;		
			}
			refresh_visual ();
		}
		
		/* Wallpaper changed */
		else if (at == server.atom._XROOTPMAP_ID)
		{
			XFreePixmap (server.dsp, server.pmap);
			server.got_root_pmap = 0;
			set_refresh_all ();
			refresh_visual ();
		}
	}
	
	else
	{
		Task *tsk;
		tsk = task_get_task (win);
		if (!tsk)
			return;
			
		/* Title */
		if (at == server.atom._NET_WM_VISIBLE_NAME)
		{
			if (config.task_show_text)
			{
				if (tsk->title)
					XFree (tsk->title);
				tsk->title = server_get_property (win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
				tsk->need_refresh = 1;
				refresh_visual ();	
			}
		}

		/* Iconic state */
		else if (at == server.atom.WM_STATE)
			tsk->iconified = window_is_iconified (win);
		
		/* Icon changed */
		else if (at == server.atom._NET_WM_ICON)
		{
			if (tsk->icon_data != 0)
				XFree (tsk->icon_data);
			tsk->need_refresh = 1;
			tsk->icon_data = 0;
			refresh_visual ();
		}
			
		if (!server.got_root_win)
			server.root_win = RootWindow (server.dsp, server.screen);
	}
}
